﻿using System;
using Quartz;
using Quartz.Spi;

namespace Bean.Quartz.Core
{
    /// <summary>
    /// 定时器 IOC工厂
    /// </summary>
    public class QuartzIocFactory : IJobFactory
    {
        private readonly IServiceProvider _serviceProvider;

        public QuartzIocFactory(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            return _serviceProvider.GetService(bundle.JobDetail.JobType) as IJob;
        }

        public void ReturnJob(IJob job)
        {
            var disposable = job as IDisposable;
            disposable?.Dispose();
        }

    }
}
